'use strict';

const uuid = require('uuid');
const redis = require('../../../external/redisClient');

const ID_PRE = 'user-token:'
const AUTH_KEY = 'builder-token';


/**
 * identify user by code
 * @param {string} code
 * @returns {string?}
 */
async function identifyUserCode(code) {
  return '';
}

/**
 * identify user by user-password
 * @param {string} userId
 * @param {string} userPassword
 * @returns {string?}
 */
async function identifyUserPassword(userId, userPassword) {
  const fakeCode = uuid.v4();
  const token = uuid.v4();
  await redis.setAsyncSafe(`${ID_PRE}${fakeCode}`, token);
  return token;
}


/**
 * @typedef {{ success: boolean, authKey: string, authToken: string, refresh?: boolean }} AuthResult 
 */
/**
 * 
 * @param {*} param0
 * @returns {AuthResult}
 */
async function checkIdentity({ userId, userCode, userPassword }) {
  const result = {
    success: false, authKey: AUTH_KEY, authToken: '', refresh: false,
  };
  const token = userToken ? (await identifyUserCode(userCode)) : (await identifyUserPassword(userId, userPassword));
  if (token) {
    result.success = true;
    result.authToken = token;
  }
  return result;
}

/**
 * 
 * @param {*} cookies
 * @returns {AuthResult}
 */
async function checkCookie(cookies) {
  const result = {
    success: false, authKey: AUTH_KEY, authToken: '', refresh: false,
  };
  if (!cookies || !cookies[AUTH_KEY]) {
    return result;
  }
}


Object.assign(module.exports, {
  checkIdentity,
  checkCookie,
});
